﻿package com.winglish.as3.utils
{
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author charmjin
	 */
	public class StringUtil
	{
		public function StringUtil() { }

		/**
		 * findTextCoordinate
		 * @param	textfield
		 * @param	textVarName
		 * @param	find
		 * @param	startIndex
		 * @return
		 */
		static public function findTextCoordinate( textfield: TextField, textVarName: String, find: String, startIndex: int = 0 ): Rectangle {
			var index: int = textVarName.indexOf( find, startIndex );
			if( index != -1 ){
				var rect01: Rectangle = textfield.getCharBoundaries( index );
				var rect02: Rectangle = textfield.getCharBoundaries( index + ( find.length - 1 ) );				
				var rect03: Rectangle = new Rectangle( rect01.x, rect01.y - 1 , rect02.x + rect02.width - rect01.x, rect01.height + 2 );
				return rect03;
			}else{
				throw new Error( "not find String" );
				return null;
			}			
		}
		
		/**
		 * 특정 문자를 찾아 다른 문자로 바꿔주는 함수.
		 * 
		 * @param	str
		 * @param	find
		 * @param	replace
		 * @return
		 */
		static public function strReplace( str: String, find: String, replace: String ) :String{
			return str.split( find ).join( replace );
		}
		
		/**
		 * 파라미터로 받은 문자열을 제거하는 함수.
		 * 
		 * @param	str
		 * @param	... replaceChar
		 * @return
		 */
		static public function strRemove(str:String, ... replaceChar):String {
			var str:String = str;
			for (var i:int = 0; i < replaceChar.length; i++) {
				str = strReplace(str, replaceChar[i], "");	
			}
			return str;
		}
		/**
		 * 텍스트 Format을 바꾸는 함수.
		 * find 값이 없으면 텍스트필드의 전체 텍스트가 적용된다. count 값이 있으면 해당 순서로 나온 find 텍스트만 적용된다.
		 * 
		 * @param	textfield
		 * @param	obj Format 설정.
		 * @param	find 변경할 텍스트
		 * @param	count 같은 변경할 텍스트라도 다르게 특정 순서에 있는 텍스트만 적용할 카운트 값.
		 */
		static public function setChangeTextFormat( textfield: TextField, obj: Object = null, find: String = null, count: * = null): void {
			var startIndex: int, endIndex: int;
			var format: TextFormat = textfield.getTextFormat();
			var str: String = textfield.text;
			
			if( find == "" ) return;
			for( var i: Object in obj ){
				if ( format.hasOwnProperty( i ) ) format[ i ] = obj[ i ];
				else trace( i, "라는 속성은 없습니다." );
			}
			if( find == null ){
				startIndex = 0;
				endIndex = textfield.text.length;
				textfield.setTextFormat( format, startIndex, endIndex );
			}else {
				var num:int = 0;
				while ( startIndex !== -1 ) {
					startIndex = str.indexOf( find, endIndex );
					endIndex = startIndex + find.length;
					if ( startIndex === -1 ) return;
					if (count == null) {
						textfield.setTextFormat( format, startIndex, endIndex );
					}else {
						num++;
						if (typeof count == "object") {
							if (count.indexOf(num) != -1) textfield.setTextFormat( format, startIndex, endIndex );
						}else if (typeof count == "number") {
							if (count == num) {
								textfield.setTextFormat( format, startIndex, endIndex );
								break;
							}
						}else {
							trace("Array or Number type use")
						}
					}
				}
			}
		}
		
		
		/**
		 * getTxtHeight
		 * @param	tf
		 * @param	tfBasicHeight
		 * @return
		 */
		static public function getTxtHeight( tf:TextField, tfBasicHeight: Number ): Number{
			var nl: int = tf.numLines
			var txtH: Number = tfBasicHeight * nl;
			return txtH;
		}
		


		static private const TAB: int = 9;
		static private const LINEFEED: int = 10;
		static private const CARRIAGE: int = 13;
		static private const SPACE: int = 32;
		/**
		 * LTrim
		 * @param	str
		 * @return
		 */
		static public function LTrim( str: String ): String{
			var s: String = str.toString();
			var i: int = 0;
			while ( s.charCodeAt( i ) == SPACE || s.charCodeAt( i ) == CARRIAGE || s.charCodeAt( i ) == LINEFEED || s.charCodeAt( i ) == TAB ){
				i++;
			}
			return s.substring( i, s.length );
		}
		
		/**
		 * RTrim
		 * @param	str
		 * @return
		 */
		static public function RTrim( str: String ): String{
			var s: String = str.toString();
			var i: int = s.length - 1;
			while ( s.charCodeAt( i ) == SPACE || s.charCodeAt( i ) == CARRIAGE || s.charCodeAt( i ) == LINEFEED || s.charCodeAt( i ) == TAB ){
				i--;
			}
			return s.substring( 0, i + 1 );
		}
		
		/**
		 * str 값이 없으면 ""로 리턴한다.
		 * @param	str
		 * @param	reStr
		 * @return
		 */
		static public function replaceEmpty(str:*, reStr:String = ""):String {
			if (str == null || str == "undefined") return reStr;
			else return str;
		}
		
		/**
		 * 한글만 textFormat 형식을 다르게한다.
		 * @param	txtFiled
		 * @param	obj
		 */
		static public function korChangeFormat(txtField:TextField, obj:Object):void {
			var str:String = txtField.text;
			var ary:Array = ArrayUtil.removeOverlapData(str.match(Pattern.KOR));
			if (ary.length > 0) {
				for (var i:uint = 0; i < ary.length; i++) {
					setChangeTextFormat(txtField, obj, ary[i]);	
				}
			}
		}
		
		/**
		 * 영문만 textFormat 형식을 다르게한다.
		 * @param	txtFiled
		 * @param	obj
		 */
		static public function engChangeFormat(txtField:TextField, obj:Object):void {
			var str:String = txtField.text;
			var ary:Array = ArrayUtil.removeOverlapData(str.match(Pattern.ENG));
			if (ary.length > 0) {
				for (var i:uint = 0; i < ary.length; i++) {
					setChangeTextFormat(txtField, obj, ary[i]);	
				}
			}
		}

	}
}